<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use app\model\Weighbill as WM;
use app\model\Users as UM;
use app\model\Truck as TM;
use app\model\Customer as CM;
use app\model\Goods as GM;
use app\model\Excavator as EM;
use app\model\House as HM;
use app\model\Trucktype as TPM;
use app\model\Position as PM;
use app\model\Route as RM;
use app\model\Config as CFM;
class Apps extends Controller
{
    public function index(){
        $houseId = session("TX_HOUSE.houseId");
        $houseName = session("TX_HOUSE.houseName");
        if($houseId <= 0){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
    	$data['data']['excavator_list'] = getExcavatorList();
        $data['data']['customer_list'] = getCustomerList();
        $data['data']['house_list'] = getHouseList();
        $data['data']['truck_type_list'] = getTruckTypeList();
        $data['data']['truck_code_list'] = getTruckCodeList();
        $data['data']['truck_list'] = getTruckList();
        $data['data']['config'] = getConfigByHouse($houseId);
        $data['data']['user_info'] = session('TX_USERS');
        $data['data']['customer_receive_list'] = getCustomerList($houseId);
        $data['data']['goods_list'] = goodsList();
        $data['data']['houseId'] = $houseId;
        $data['data']['houseName'] = $houseName;
        $data['data']['user_list'] = getUserList();
        $data['data']['config_list'] = getAllConfig();
        $data['data']['route_list'] = getRouteList();
        $data['data']['position_list'] = getPositionList();
        $data['time'] = time();

        return $data;
    }

	/***
	*
	*磅房相关接口
	*
	***/

	/***
	*
	*获取磅房列表
	*
	***/
    public function houseList(){
    	$rs = getHouseList();
    	$data['status'] = 1;
    	$data['msg'] = 'ok';
    	$data['data'] = $rs;
    	return $data;
    }


        /***
    *
    *保存磅房
    *
    ***/
    public function houseSave(){
        $pd = input();
        $houseId = 0;
        if(isset($pd['houseId']) && !empty($pd['houseId'])){
            $houseId = $pd['houseId'];
        }
        if(!isset($pd['houseName']) || empty($pd['houseName'])){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new HM();
        unset($pd['houseId']);
        if(empty($houseId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('houseId'=>$houseId))->update($pd);
            $pd['houseId'] = $houseId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除磅房
    *
    ***/
    public function houseDel(){
        $houseId = input('houseId/d');
        if(empty($houseId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $houseId;
            return $data;
            exit;
        }
        $m = new HM();
        $rs = $m->where(array('houseId'=>$houseId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }





	/***
	*
	*配置相关接口
	*
	***/


	/***
	*
	*获取配置列表
	*
	***/
    public function configList(){
    	$rs = getConfig();
    	$data['status'] = 1;
    	$data['msg'] = 'ok';
    	$data['data'] = $rs;
    	//print_r($data);
    	return $data;
    }


    public function getAllConfigList(){
        $rs = getAllConfig();
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
    }



	/***
	*
	*根据配置ID查找配置
	*
	***/
    public function configById(){
    	$id = input('configId/d');
    	if(empty($id)){
    		$data['status'] == 0;
    		$data['msg'] = "参数错误";
    		$data['data'] = '';
    		return $data;
    		exit;
    	}
    	$rs = getConfig($id);
    	$data['status'] = 1;
    	$data['msg'] = 'ok';
    	$data['data'] = $rs;
    	//print_r($data);
    	return $data;
    }

    /***
	*
	*根据磅房ID查找配置
	*
	***/
    public function configByHouseId(){
    	$id = input('houseId/d');
    	if(empty($id)){
    		$data['status'] == 0;
    		$data['msg'] = "参数错误";
    		$data['data'] = '';
    		return $data;
    		exit;
    	}
    	$rs = getConfigByHouse($id);
    	$data['status'] = 1;
    	$data['msg'] = 'ok';
    	$data['data'] = $rs;
    	//print_r($data);
    	return $data;
    }



    /***
    *
    *保存配置
    *
    ***/
    public function configSave(){
        $pd = input();
        $configId = 0;
        if(isset($pd['configId']) && !empty($pd['configId'])){
            $configId = $pd['configId'];
        }
        $m = new CFM();
        unset($pd['configId']);
        if(empty($configId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('configId'=>$configId))->update($pd);
            $pd['configId'] = $configId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除配置
    *
    ***/
    public function configDel(){
        $configId = input('configId/d');
        if(empty($configId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $configId;
            return $data;
            exit;
        }
        $m = new CFM();
        $rs = $m->where(array('configId'=>$configId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }




    /***
    *
    *用户相关接口
    *
    ***/

    /***
    *
    *获取用户列表
    *
    ***/
    public function getAllUserList(){
        $rs = getUserList();
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
    }

    /***
    *
    *重置用户密码
    *
    ***/
    public function resetPassword(){
        $id = input('userId/d');
        if(empty($id)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $userInfo = session('TX_USERS');
        if($userInfo['userId'] == $id){
            $data['status'] = -1;
            $data['msg'] = '不能得置自己的密码';
            $data['data'] = '';
            return $data;
            exit;
        }
        $m= new UM();
        $info = $m->where(array('userId'=>$id))->find();
        if(empty($info)){
            $data['status'] = -1;
            $data['msg'] = '查找用户信息失败';
            $data['data'] = '';
            return $data;
            exit;
        }
        $pwd = md5('123456' . md5('123456'));
        if($pwd == $info['userPwd']){
            $data['status'] = 1;
            $data['msg'] = '重置用户密码成功';
            $data['data'] = '';
            return $data;
            exit;
        }
        $rs = setUserPwd($id);
        if(false == $rs){
            $data['status'] = -1;
            $data['msg'] = '重置用户密码失败';
        }else{
            $data['status'] = 1;
            $data['msg'] = '重置用户密码成功';
        }
        $data['data'] = '';
        return $data;
    }


    /***
    *
    *修改用户密码
    *
    ***/

    public function setPassword(){
        $id = input('userId/d');
        $oldPwd = input('oldPwd');
        $pwd = input('userPwd');
        if(empty($id) || empty($oldPwd) || empty($pwd) || ($oldPwd == $pwd)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $chk = checkUserPwd($id,$oldPwd);
        if(empty($chk)){
            $data['status'] = -1;
            $data['msg'] = '用户旧密码错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $rs = setUserPwd($id,$pwd);
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = '设置密码失败';
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = '修改密码成功';
        $data['data'] = '';
        return $data;
    }


    /***
    *
    *设置用户权限
    *
    ***/

    public function setUserPower(){
        $userId = input('userId/d');
        if(empty($userId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $userId;
            return $data;
            exit;
        }
        $power = input('power');
        $user = new UM();
        $rs = $user->where(array('userId'=>$userId))->update(['power'=>$power]);        
        $ts = $user->getError();
        if(empty($rs) && !empty($ts)){
            $data['status'] = -1;
            $data['msg'] = $ts;
            $data['data'] = '';
            return $data;
            exit;
        }
        $ses = session('TX_USERS');
        if($ses['userId'] == $userId){
            session('TX_USERS.power',$power);
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $power;
        $data['ses'] = $ses;
        return $data;
    }


    /***
    *
    *新增用户
    *
    ***/
    public function addUsers(){
        $userInfo = session('TX_USERS');
        if($userInfo['isManager'] != 1){
            $data['status'] = -1;
            $data['msg'] = '您没有权限新增用户';
            $data['data'] = '';
            return $data;
            exit;
        }
        $pd['userName'] = input('userName');
        $pd['userPwd'] = input('userPwd');
        $pd['isManager'] = input('isManager/d');
        $pk = 1;
        if(empty($pd['userName'])){
            $pk = 0;
        }
        if(empty($pd['userPwd'])){
            $pk = 0;
        }
        if(empty($pk)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $m = new UM();
        $rs = $m->create($pd);
        if(!$rs){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $pd['userId'] = $rs['userId'];
        $pd['power'] = '';
        $pd['dataFlag'] = 1;
        unset($pd['userPwd']);
        $data['data'] = $pd;
        return $data;
    }

    /***
    *
    *删除用户
    *
    ***/
    public function deleteUsers(){
        $id = input('userId/d');
        if(empty($id)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $userInfo = session('TX_USERS');
        if(empty($userInfo['userId'])){
            $data['status'] = -1;
            $data['msg'] = '登录信息错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $user = new UM();
        $rs = $user->where(array('userId'=>$id))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = '删除用户失败:' . $user->getError();
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = '删除用户成功';
        $data['data'] = $id;
        return $data;
    }

	/***
	*
	*客户相关接口
	*
	***/
    public function custmerList(){
    	$rs = getCustomerList();
    	$data['status'] = 1;
    	$data['msg'] = 'ok';
    	$data['data'] = $rs;
    	//print_r($data);
    	return $data;
    }


    /***
    *
    *保存客户信
    *
    ***/
    public function customerSave(){
        $pd = input();
        $m = new CM();
        if(empty($pd['customerId'])){
            $md = 0;
            unset($pd['customerId']);
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = '';
                //print_r($data);
                return $data;
                exit;
            }
            /*$pd['customerId'] = $rs['customerId'];*/
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = 0;
            //print_r($data);
            return $data;
            exit;
        }else{
            $md = 1;
            $where['customerId'] = $pd['customerId'];
            unset($pd['customerId']);            
            $rs = $m->where($where)->update($pd);
            $sd = $m->getError();
            if(empty($rs) && !empty($sd)){
                $data['status'] = -1;
                $data['msg'] = $sd;
                $data['data'] = '';
                //print_r($data);
                return $data;
                exit;
            }else{
                $pd['customerId'] = $where['customerId'];
                $data['status'] = 1;
                $data['msg'] = 'ok';
                $data['data'] = $pd;
                $data['model'] = 1;
                //print_r($data);
                return $data;
                exit;
            }
        } 
    }



    /***
    *
    *删除客户信息
    *
    ***/

    public function customerDel(){
        $customerId = input('customerId/d');
        if(empty($customerId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $customerId;
            return $data;
            exit;
        }
        $m = new CM();
        $rs = $m->where(array('customerId'=>$customerId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $customerId;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $customerId;
        return $data;
    }



	/***
	*
	*钩机号相关接口
	*
	***/
    public function excavatorList(){
    	$rs = getExcavatorList();
    	$data['status'] = 1;
    	$data['msg'] = '';
    	$data['data'] = $rs;
    	return $data;
    }



    /***
    *
    *保存钩机号
    *
    ***/
    public function excavatorSave(){
        $pd = input();
        $excavatorId = 0;
        if(isset($pd['excavatorId']) && !empty($pd['excavatorId'])){
            $excavatorId = $pd['excavatorId'];
        }
        if(!isset($pd['excavatorCode']) || empty($pd['excavatorCode'])){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new EM();
        unset($pd['excavatorId']);
        if(empty($excavatorId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('excavatorId'=>$excavatorId))->update($pd);
            $pd['excavatorId'] = $excavatorId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除钩机号
    *
    ***/
    public function excavatorDel(){
        $excavatorId = input('excavatorId/d');
        if(empty($excavatorId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $excavatorId;
            return $data;
            exit;
        }
        $m = new EM();
        $rs = $m->where(array('excavatorId'=>$excavatorId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }


    /***
    *
    *获取磅单编号
    *
    ***/
    public function codeNumber(){
        $codeLeft = input('codeLeft');
        if(empty($codeLeft)){
            $data['status'] = -1;
            $data['msg'] = '创建磅单编号错误,磅单左字符不正确';
            $data['data'] = '';
            return $data;
            exit;
        }
        $dstr = date('Ymd');
        $rs = getSerialNumber();
        if($rs['status'] != 1){
            $data['status'] = -1;
            $data['msg'] = '创建磅单编号错误,' . $rs['msg'];
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $codeLeft . $dstr . $rs['data'];
        return $data;
    }



    /***
    *
    *车辆类型相关接口
    *
    ***/


    /***
    *
    *获取车辆类型列表
    *
    ***/
    public function truckTypeList(){
        $list = getTruckTypeList();
        if(empty($list)){
            $list = array();
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $list;
        return $data;
    }


    /***
    *
    *保存车辆类型
    *
    ***/
    public function trucktypeSave(){
        $pd = input();
        $typeId = 0;
        if(isset($pd['typeId']) && !empty($pd['typeId'])){
            $typeId = $pd['typeId'];
        }
        if(!isset($pd['typeName']) || empty($pd['typeName'])){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new TPM();
        unset($pd['typeId']);
        if(empty($typeId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('typeId'=>$typeId))->update($pd);
            $pd['typeId'] = $typeId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除车辆类型
    *
    ***/
    public function trucktypeDel(){
        $typeId = input('typeId/d');
        if(empty($typeId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $typeId;
            return $data;
            exit;
        }
        $m = new TPM();
        $rs = $m->where(array('typeId'=>$typeId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }





    /***
    *
    *获取车辆号牌列表
    *
    ***/
    public function truckCodeList(){
        $list = getTruckCodeList();
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $list;
        return $data;
    }



    /***
    *
    *获取车辆列表
    *
    ***/
    public function truckList(){
        $list = getTruckList();
        $data['status'] = 1;
        $data['msg'] = 'ok' . '1';
        $data['data'] = $list;
        //print_r($data);
        return $data;
    }


    /***
    *
    *新增车辆
    *
    ***/
    public function truckAdd(){
        $userInfo = session('TX_USERS');
        $pd['truckCode'] = input('truckCode');
        $pd['truckOwner'] = input('truckOwner');
        $pd['truckType'] = input('truckType/d');
        $pd['driver'] = input('driver');
        $pd['dataFlag'] = input('dataFlag/d');
        $pd['truckCode'] = strtoupper($pd['truckCode']);
        $pk = 1;
        if(empty($pd['truckCode'])){
            $pk = 0;
        }
        if(empty($pd['truckOwner'])){
            $pk = 0;
        }
        if(empty($pd['truckType'])){
            $pk = 0;
        }
        if(empty($pd['driver'])){
            $pk = 0;
        }
        if(empty($pk)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new TM();
        $rs = $m->create($pd);
        if(!$rs){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $pd['truckId'] = $rs['truckId'];
        $data['data'] = $pd;
        return $data;
    }



    /***
    *
    *编辑车辆信息
    *
    ***/

    public function truckSave(){
        $truckId = input('truckId/d');
        if(empty($truckId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $pd = input();
        $pk = 1;
        if(empty($pd['truckCode'])){
            $pk = 0;
        }
        if(empty($pd['truckOwner'])){
            $pk = 0;
        }
        if(empty($pd['truckType'])){
            $pk = 0;
        }
        if(empty($pd['driver'])){
            $pk = 0;
        }
        if(empty($pk)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $pd['truckCode'] = strtoupper($pd['truckCode']);
        $m = new TM();
        $rs = $m->where(['truckId'=>$truckId])->update($pd);
        $ts = $m->getError();
        if(empty($rs) && !empty($ts)){
            $data['status'] = -1;
            $data['msg'] = $ts;
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $pd;
        return $data;
    }


    /***
    *
    *删除车辆信息
    *
    ***/
    public function truckDel(){
        $truckId = input('truckId/d');
        if(empty($truckId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $truckId;
            return $data;
            exit;
        }
        $m = new TM();
        $rs = $m->where(['truckId'=>$truckId])->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $truckId;
        return $data;
    }

    /***
    *
    *根据车牌号码查找车辆信息
    *@weighmodel > 0为预存车皮否则为一车一回皮
    *@weightype > 0为毛重否则为皮重
    *@truckAdd:true新增车辆信息
    *@insert:true为新增过榜记录
    ***/

    /*不同磅房预存车皮不同*/
    public function truckByCode(){
        $code = input('truckCode');
        $code = strtoupper($code);
        $houseId = input('houseId/d');
        if($houseId <= 0){
            $data['status'] = -1;
            $data['msg'] = '磅房ID错误,请联系管理员';
            $data['data'] = '';
            $data['houseId'] = $houseId;
            $data['insert'] = false;
            $data['truckAdd'] = false;
            return $data;
            exit;
        }
        $weighInterval = input('weighInterval/d');
        $timeAreareport = input('timeAreareport/d');
        $weighmodel = input('weighmodel/d');
        $weightype = input('weightype/d');
        $tmp['emptyWeigh'] = 0;
        $tmp['fullWeigh'] = 0;
        $tmp['weigh'] = 0;
        $tmp['position'] = '';
        $tmp['billCode'] = '';
        $tmp['billId'] = 0;
        $where['t.dataFlag'] = 1;
        $where['t.truckCode'] = $code;
        /*if($weighmodel == 0){
            $where['t.houseId'] = $houseId;
        }*/
        $rs = Db::name('truck')->alias('t')->join('__TRUCKTYPE__ m','t.truckType=m.typeId')->where($where)->field('t.truckId,t.truckCode,t.driver,t.emptyWeigh,t.weighDate as billDate,m.typeName as truckType,m.maxWeigh')->find();
        if(empty($rs)){
            $tsk = array('billId'=>0,'billCode'=>'','billDate'=>date('Y-m-d',time()),'fullOperator'=>'','emptyOperator'=>'','emptyWeigh'=>0,'fullWeigh'=>0,'weigh'=>0);
            $data['status'] = 1;
            if($weighmodel == 0){
                $data['status'] = -1;
                $data['msg'] = '没有此车辆或没有预存车皮';
            }else{
                $data['msg'] = '没有此车辆';
            }
            $data['data'] = $tsk;
            $data['insert'] = true;
            $data['truckAdd'] = true;
            $data['weighmodel'] = $weighmodel;
            $data['truckCode'] = $code;
            $data['weighType'] = $weightype;
            $data['test'] = $rs;
            $data['where'] = $where;
            //print_r($data);
            return $data;
            exit;
        }else{
            /*if($weighmodel == 0 && $weightype > 0){
                $rs['billDate'] = date('Y-m-d',$rs['billDate']);
            }*/
            if($weighInterval != 0){
                $d = strtotime(date('Y-m-d',time()));
                $as = Db::name('weighbill')->where(array('billDate'=>$d,'truckCode'=>$code))->field('max(fullTime) as fullTime,emptyTime,billId,billDate,truckCode')->find();
                if($weighmodel == 0){
                    $sa = (time() - $as['fullTime']) >= $weighInterval * 60 ? true : false;
                }else{
                    /*判断一下车车回皮情况下两车之间的时间距离*/
                    $sa = false;
                    if($weightype == 0){/*毛重*/
                        $sa = (time() - $as['fullTime']) >= $weighInterval * 60 ? true : false;
                    }else{
                        $sa = (time() - $as['emptyTime']) >= $weighInterval * 60 ? true : false;
                    }
                    
                }                
                if(!empty($as) && !$sa){
                    $data['status'] = -2;
                    $data['msg'] = '车牌号为:' . $code . '的车辆两车过磅时间过于频繁,请稍后再试';
                    $data['data'] = '';
                    $data['fullTime'] = $as['fullTime'];
                    $data['emptyTime'] = $as['emptyTime'];
                    $data['time'] = time();
                    $data['sa'] = $sa;
                    $data['model'] = $weightype;
                    return $data;
                    exit;
                }
            }
            
            if($weighmodel == 0){
                /*预存车皮模式下，如果空车重量为0，则报错。*/
                if(empty($rs['emptyWeigh']) && $weightype == 0){
                    $data['status'] = -2;
                    $data['msg'] = '此车辆没有预存皮重';
                    $data['data'] = $rs;
                    return $data;
                    exit;
                }
                unset($tmp['emptyWeigh']);
                if($weightype == 0){
                    $data['status'] = 1;
                    $data['msg'] = 'ok';
                    $data['data'] = array_merge($rs,$tmp);
                    $data['insert'] = true;
                }else{
                    $data['status'] = 1;
                    $data['msg'] = 'ok';
                    $data['data'] = $rs;
                    $data['insert'] = false;
                }
                $data['truckAdd'] = false;
                $data['weighmodel'] = $weighmodel;
                $data['truckCode'] = $code;
                $data['weighType'] = $weightype;
                $data['test'] = 2;
                //print_r($data);
                return $data;
                exit;
            }else{
                unset($rs['emptyWeigh']);
                unset($tmp['position']);
                $asp = scaleIsLegal($code,$houseId,$weightype);
                if($asp['status'] < 0){
                    $data['status'] = 1;
                    $data['msg'] = 'ok';
                    $data['data'] = array_merge($rs,$tmp);
                    $data['insert'] = true;
                    $data['truckAdd'] = false;
                    $data['test'] = 3;
                    $data['asp'] = $asp;
                    //print_r($data);
                    return $data;
                    exit;
                }else if($asp['status'] == 0){
                    $data['status'] = -1;
                    $data['msg'] = $asp['msg'];
                    $data['data'] = $tmp;
                    $data['insert'] = false;
                    $data['truckAdd'] = false;
                    $data['test'] = 4;
                    $data['asp'] = $asp;
                    //print_r($data);
                    return $data;
                    exit;
                }else{
                    $data['status'] = 1;
                    $data['msg'] = 'ok';
                    $data['data'] = array_merge($rs,$asp['data']);
                    $data['insert'] = false;
                    $data['truckAdd'] = false;
                    $data['test'] = 5;
                    $data['asp'] = $asp;
                    //print_r($data);
                    return $data;
                    exit;
                }
            }
        }
    }


    /***
    *
    *检查磅单编号是否可用
    *
    ***/
    public function checkBillCode(){
        $billCode = input('billCode');
        $insert = input('insert');
        if(empty($billCode)){
            $data['status'] = -1;
            $data['msg'] = '磅单编号不能为空';
            $data['data'] = '';
            $data['billCode'] = $billCode;
            return $data;
            exit;
        }
        if($insert == 'false'){
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $billCode;
            return $data;
        }
        $rs = Db::name('weighbill')->where(array('dataFlag'=>1,'billCode'=>$billCode))->find();
        if(!empty($rs)){
            $data['status'] = -1;
            $data['msg'] = '磅单编号重复,请生新输入车牌号码';
            $data['data'] = '';
            $data['insert'] = $insert;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $billCode;
        return $data;
    }


    /***
    *
    *保存磅单
    *@weighmodel = 0为预存车皮否则为一车一回皮
    *@weightype ＝ 0为毛重否则为皮重
    *@truckAdd:true新增车辆信息
    *@insert:true为新增过榜记录
    *
    ***/
    public function saveBill(){
        $vd = input();
        if(isset($vd['truckCode'])){
            $vd['truckCode'] = strtoupper($vd['truckCode']);
        }
        $weighModel = $vd['weighModel'];
        $weighType = $vd['weightype'];
        if($weighType > 0 || empty($vd['picUrl'])){
            unset($vd['picUrl']);
        }
        $isInsert = $vd['insert'];
        $vd['billDate'] = strtotime(date('Y-m-d',time()));
        if($weighModel > 0){
            if($weighType > 0){
                $vd['emptyTime'] = $vd['fullTime'] = time();
            }else{
                $vd['emptyTime'] = time();
            }
        }else{
            if($weighType > 0){
                $vd['fullTime'] = time();
                $vd['emptyTime'] = $vd['emptyTime'];
            }else{
                $vd['emptyTime'] = time();
                $vd['fullTime'] = $vd['emptyTime'];
            }
        }
        /*$vd['emptyTime'] = floor($vd['emptyTime'] / 1000);
        $vd['fullTime'] = floor($vd['emptyTime'] / 1000);*/
        if((int)$vd['emptyWeigh'] > 0 && (int)$vd['fullWeigh'] > 0){
            $vd['weigh'] = abs($vd['fullWeigh'] - $vd['emptyWeigh']);
        }
        $weighbill = new WM();
        unset($vd['weighModel']);
        unset($vd['weightype']);
        unset($vd['insert']);
        if($weighModel == 0){
            if($weighType == 0){//预存车皮之毛重
                $rs = $weighbill->insertGetId($vd);
                if(empty($rs)){
                    $data['status'] = -1;
                    $data['msg'] = $weighbill->getError();
                    $data['data'] = $rs;
                    return $data;
                    exit;
                }
                $data['status'] = 1;
                $data['msg'] = 'ok';                
                if($vd['weigh'] > 0){
                    $iscanprint = true;                    
                }else{
                    $iscanprint = false;
                }
                
                $t = $vd['emptyTime'];
                $vd['emptyTime'] = date('H:i:s',$t);
                /*需加一段转换检重时间格式的代码*/
                if($iscanprint == true){                    
                   $t1 = $vd['fullTime'];                    
                    $vd['fullTime'] = date('H:i:s',$t1);
                    $vd['billDate'] = date('Y-m-d',$vd['billDate']);
                }
                $vd['billId'] = $rs;
                $vd['printCount'] = 0;
                $data['data'] = $vd;
                $data['canprint'] = $iscanprint;
                return $data;
                exit;
            }else{//预存车皮之皮重
                $truckCode = $vd['truckCode'];
                $arr['emptyWeigh'] = $vd['emptyWeigh'];
                $arr['weighDate'] = time();
                $arr['emptyOperator'] = $vd['emptyOperator'];
                $t = new Truck();
                $rs = $t->setEmpty($truckCode,$arr);
                if(empty($rs)){
                    $data['status'] = -1;
                    $data['msg'] = $t->getError();
                    $data['data'] = $rs;
                    return $data;
                    exit;
                }
                $data['status'] = 1;

                $data['msg'] = 'ok';
                /*加转换时间格式*/
                $data['data'] = $rs;
                return $data;
                exit;
            }
        }else{
            /*车车回皮*/
            if($weighType == 1){
                $vd['emptyTime'] = time();
            }else{
                $vd['fullTime'] = time();
            }
            if(empty($vd['billId'])){
                unset($vd['billId']);
                $rs = $weighbill->insertGetId($vd);
                if(empty($rs)){
                    $data['status'] = -1;
                    $data['msg'] = $weighbill->getError();
                    $data['data'] = $rs;
                    return $data;
                    exit;
                }
                $vd['billId'] = $rs;
            }else{
                $billId = $vd['billId'];
                unset($vd['billId']);
                $rs = $weighbill->where(array('billId'=>$billId))->update($vd);
                if(empty($rs)){
                    $data['status'] = -1;
                    $data['msg'] = $weighbill->getError();
                    $data['data'] = $rs;
                    return $data;
                    exit;
                }
            }
            $data['status'] = 1;
            $data['msg'] = '保存成功';
            $data['weightype'] = $weighType;
            if($vd['weigh'] > 0){
                $data['canprint'] = true;
                $rs['billDate'] = Date('Y-m-d',$vd['billDate']);
                /*需加一段转换检重时间格式的代码*/
            }else{
                $data['canprint'] = false;
            }
            if(!empty($vd['emptyTime'])){
                $t = $vd['emptyTime'];
                $vd['emptyTime'] = date('H:i:s',$t);
            }else{
                $vd['emptyTime'] = '';
            }
            if(!empty($vd['fullTime'])){
                $t = $vd['fullTime'];
                $vd['fullTime'] = date('H:i:s',$t);
            }else{
                $vd['fullTime'] = '';
            }
            if($data['canprint'] == true){
                $rs['fullTime'] = $vd['fullTime'];
                $rs['emptyTime'] = $vd['emptyTime'];
                $rs['printCount'] = 0;
            }
            $data['data'] = $rs;
            return $data;
        }
    }


    /***
    *
    *编辑磅单
    *
    ***/
    public function editBill(){
        $arr = input();
        $id = $arr['billId'];
        if(empty($id)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $weighbill = new WM();
        unset($arr['billId']);
        $rs = $weighbill->where(array('billId'=>$id))->update($arr);
        if(!$rs){
            $data['status'] = -1;
            $data['msg'] = $weighbill->getError();
            $data['data'] = $rs;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $arr['billId'] = $id;
        $data['data'] = $arr;
        return $data;
    }


    /***
    *
    *删除磅单
    *
    ***/
    public function delBill(){
        $arr = input();
        $md = $arr['md'];
        $id = $arr['billId'];
        if(empty($id)){
            $data['status'] = -1;
            $data['msg'] = "参数错误";
            $data['data'] = '';
            return $data;
            exit;
        }
        $where['billId'] = $id;
        $m = new WM();
        try {
            if($md == 1){
                $v['dataFlag'] = 0;
            }else if($md == 2){
                $v['fullWeigh'] = 0;
                $v['weigh'] = 0;
            }else if($md == 3){
                $v['emptyWeigh'] = 0;
                $v['weigh'] = 0;
            }else if($md == 4){
                $v['emptyWeigh'] = $arr['fullWeigh'];
                $v['fullWeigh'] = $arr['emptyWeigh'];
            }
            $rs = $m->where($where)->update($v);
            if(!$rs){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            unset($arr['md']);
            $data['data'] = $arr;
            return $data;
        } catch (Exception $e) {
            $data['status'] = -1;
            $data['msg'] = $e;
            $data['data'] = '';
            return $data;
        }
        
    }


    /***
    *
    *预存皮重
    *
    ***/

    public function saveTruckEmpty(){
        $truckCode = input('truckCode');
        if(empty($truckCode)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = input();
            return $data;
            exit;
        }
        $arr = input();
        if($arr['weighDate']){
            $arr['weighDate'] = strtotime($arr['weighDate']);
        }else{
            $arr['weighDate'] = time();
        }
        unset($arr['truckId']);
        $t = new TM();
        $rs = $t->setEmpty($truckCode,$arr);
        $ks = $t->getError();
        if(empty($rs) && !empty($ks)){
            $data['status'] = -1;
            $data['msg'] = $ks;
            $data['data'] = input();
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $arr;
        return $data;
    }



    /***
    *
    *打印次数自增
    *
    ***/
    public function printCountAdd(){
        $id = input('billId/d');
        if(empty($id)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = '';
            return $data;
            exit;
        }
        $rs = Db::execute("update tx_weighbill set printCount=printCount + 1 where billId=" . $id);
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = '操作失败';
            $data['data'] = $id;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
    }

    /***
    ***待交班磅单列表
    ***/
    public function getWillChangeBillList(){
        $t = strtotime(date('Y-m-d',time() - 86400));
        $houseId = input('houseId/d');
        if(empty($houseId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $houseId;
            return $data;
            exit;
        }
        $weighbill = new WM();
        $where['billDate'] = $t;
        $where['weigh'] = 0;
        $where['dataFlag'] = 1;
        $where['houseId'] = $houseId;
        $rs = $weighbill->where($where)->select();
        if(empty($rs)){
            $rs = array();
        }else{
            foreach($rs as $k => $v){
                $rs[$k]['billDateDesc'] = date('Y-m-d',$v['billDate']);
            }
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
    }



    /***
    ***交班
    ***/
    public function changeBillDate(){
        $idList = input('ids');
        if(empty($idList)){
            $data['status'] = -1;
            $data['msg'] = '没有可交班的记录';
            $data['data'] = '';
            return $data;
            exit;
        }
        $arr['billDate'] = strtotime(date('Y-m-d',time()));
        $where = 'billId in (' . $idList . ')';
        $weighbill = new WM();
        $rs = $weighbill->where($where)->update($arr);
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $weighbill->getError();
            $data['data'] = '';
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok' . $where;
        $data['data'] = $rs;
        return $data;
    }


    /***
    ***获取当日净重
    ***/
    public function getWeighList(){
        $d = input('date');
        $o = input('operator');
        $houseId = session("TX_HOUSE.houseId");
        if(empty($houseId)){
            $data['status'] = -1;
            $data['msg'] = '磅房ID错误';
            $data['data'] = $houseId;
            return $data;
            exit;
        }
        if(empty($d)){
            $d = strtotime(date('Y-m-d',time()));
        }else{
            $d = strtotime($d);
        }
        $where['w.billDate'] = $d;
        $where['w.houseId'] = $houseId;
        if(!empty($o)){
            $where['w.fullOperator'] = $o;
        }
        $where['w.emptyWeigh'] = ['>',0];
        $where['w.fullWeigh'] = ['>',0];
        $where['w.weigh'] = ['>',0];
        $where['w.dataFlag'] = 1;
        //$bw = new WM();
        //$rs = $bw->where($where)->select();
        $rs = Db::name('weighbill')->alias('w')->join("__HOUSE__ h",'w.houseId=h.houseId')->where($where)->field("w.billId,w.billCode,w.billDate,w.goodsName,w.truckCode,w.driver,w.emptyWeigh,w.emptyTime,w.emptyOperator,w.fullWeigh,w.fullTime,w.fullOperator,w.weigh,w.sendUnit,w.receiveUnit,w.position,w.excavator,w.truckType,w.mem,w.route,h.houseName,w.printCount,w.houseId,w.locked")->order('w.billId DESC')->select();
        foreach($rs as $k=>$v){
            $rs[$k]['billDateDesc'] = date('Y-m-d',$v['billDate']);
            $rs[$k]['emptyTimeDesc'] = date('H:i:s',$v['emptyTime']);
            $rs[$k]['fullTimeDesc'] = date('H:i:s',$v['fullTime']);
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        //print_r($data);
        return $data;
    }


    /***
    *获取皮重或毛重记录
    *@type 为0时获取毛重记录,为1时获取皮重记录
    *@md 为1时获取待磅车辆
    ***/
    public function getUnableBills($type=0,$sd=0){
        if($sd == 0){
            if($type == 0){
                $where['w.fullWeigh'] = ['>',0];
                $where['w.emptyWeigh'] = 0;
            }else{
                $where['w.emptyWeigh'] = ['>',0];
                $where['w.fullWeigh'] = 0;
            }
        }else{
            if($type == 0){
                $where['w.emptyWeigh'] = ['>',0];
                $whrer['w.fullWeigh'] = 0;
            }else{
                $where['w.fullWeigh'] = ['>',0];
                $whrer['w.emptyWeigh'] = 0;
            }            
        }
        $where['w.dataFlag'] = 1;
        $where['w.weigh'] = 0;
        $t = strtotime(date('Y-m-d',time()));
        $where['billDate'] = $t;
        $rs = Db::name('weighbill')->alias('w')->join("__HOUSE__ h",'w.houseId=h.houseId')->where($where)->field("w.billId,w.billCode,w.billDate,w.goodsName,w.truckCode,w.driver,w.emptyWeigh,w.emptyTime,w.emptyOperator,w.fullWeigh,w.fullTime,w.fullOperator,w.weigh,w.sendUnit,w.receiveUnit,w.position,w.excavator,w.truckType,w.mem,w.route,h.houseName,w.printCount,w.houseId,w.locked")->order('w.billId DESC')->select();
        if(empty($rs)){
            $rs = array();
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        /*print_r($data);*/
        return $data;
    }


    /***
    *
    *过磅统计
    *@sendUnit发货单位
    *@receiveUnit收货单位
    *@goodsName物资名称
    *
    ***/

    public function getCountWeigh(){
        $billDate = strtotime(date('Y-m-d',time()));
        $sendUnit = input('sendUnit');
        $receiveUnit = input('receiveUnit');
        $goodsName = input('goodsName');
        $mine = input('mine/d');
        $userInfo = session('TX_USERS');
        $houseInfo = session('TX_HOUSE');
        if(!empty($sendUnit)){
            $where['sendUnit'] = $sendUnit;
        }
        if(!empty($receiveUnit)){
            $where['receiveUnit'] = $receiveUnit;
        }
        if(!empty($goodsName)){
            $where['goodsName'] = $goodsName;
        }
        $where['billDate'] = $billDate;
        $where['houseId'] = $houseInfo['houseId'];
        try {
            $where['dataFlag'] = 1;
            $where['emptyWeigh'] = ['>',0];
            if($mine > 0){
                $where['emptyOperator'] = $userInfo['userName'];
            }
            $rs['emptyCounts'] = Db::name('weighbill')->where($where)->count();
            if(empty($rs['emptyCounts'])){
                $rs['emptyCounts'] = 0;
            }
            if(!empty($where['emptyOperator'])){
                unset($where['emptyOperator']);
            }
            unset($where['emptyWeigh']);
            $where['fullWeigh'] = ['>',0];
            if($mine > 0){
                $where['fullOperator'] = $userInfo['userName'];
            }
            $rs['fullCounts'] = Db::name('weighbill')->where($where)->count();
            if(empty($rs['fullCounts'])){
                $rs['fullCounts'] = 0;
            }
            $where['emptyWeigh'] = ['>',0];
            $where['fullWeigh'] = ['>',0];
            $where['weigh'] = ['>',0];
            $rs['weighCounts'] = Db::name('weighbill')->where($where)->count();
            $rs['weighSum'] = Db::name('weighbill')->where($where)->sum('weigh');
            $rs['weighSum'] = $rs['weighSum'] / 1000;
            if(empty($rs['weighCounts'])){
                $rs['weighCounts'] = 0;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['where'] = $where;
            return $data;
            //print_r($data);
        } catch (Exception $e) {
            $data['status'] = 0;
            $data['msg'] = $e;
            $data['data'] = array('emptyCounts'=>0,'fullCounts'=>0,'weighCounts'=>0,'weighSum'=>0.00);
            return $data;
        }
    }


    /***
    *
    *过磅单审核与解锁
    *
    ***/

    public function changeBillLocked(){
        $ids = input('ids');
        $arr['locked'] = input('locked/d');
        if(empty($ids)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $ids;
            return $data;
            exit;
        }
        $where = "billId in (" . $ids . ")";
        $m = new WM();
        $rs = $m->where($where)->update($arr);
        $sk = $m->getError();
        if(empty($rs) && !empty($sk)){
            $data['status'] = -1;
            $data['msg'] = $sk;
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $ids;
        $data['type'] = input('locked/d');
        return $data;
    }


    /***
    *
    *退出程序
    *
    ***/
    public function exitProgram(){
        session('TX_USERS',null);
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = "";
        return $data;
    }


    /***
    *
    *统计及报表
    *
    ***/

    /***
    *
    *普通报表接口
    *
    ***/
    public function getNormalReport(){
        $timeArea = input('timeArea');
        $billDate = input('billDate');
        $goodsName = input('goodsName');
        $sendUnit = input('sendUnit');
        $receiveUnit = input('receiveUnit');
        $fullOperator = input('fullOperator');
        $houseId = input('houseId');
        $billDateStart = input('billDateStart');
        $billDateEnd = input('billDateEnd');
        $billCode = input('billCode');
        $truckCode = input('truckCode');
        $position = input('position');
        $route = input('route');
        $excavator = input('excavator');
        $list = normalReport($timeArea,$billDateStart,$billDateEnd,$billDate,$goodsName,$sendUnit,$receiveUnit,$fullOperator,$billCode,$truckCode,$position,$route,$excavator,$houseId);
        /*$data['status'] = -1;
        $data['msg'] = '';
        $data['data'] = $list;
        $data['arr'] = input();
        return $data;
        exit;*/
        if(empty($list)){
            $list = array();
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        if(empty($list)){
            $data['data'] = array();
            $data['sum'] = 0;
            $data['cs'] = 0;
        }else{
            $sum = 0;
            $cs = 0;
            foreach($list as $k => $v){
                if(empty($v['emptyTime'])){
                    $list[$k]['emptyTimeDesc'] = '';
                }else{
                    $list[$k]['emptyTimeDesc'] = date('H:i:s',$v['emptyTime']);
                }
                if(empty($v['fullTime'])){
                    $list[$k]['fullTimeDesc'] = '';
                    $list[$k]['picPath'] = date('Y-m-d',time());
                }else{
                    $list[$k]['fullTimeDesc'] = date('H:i:s',$v['fullTime']);
                    $list[$k]['picPath'] = date('Y-m-d',$v['fullTime']);
                }
                if(empty($v['billDate'])){
                    $list[$k]['billDateDesc'] = '';
                }else{
                    $list[$k]['billDateDesc'] = date('Y-m-d',$v['billDate']);
                }
                $sum += $v['weigh'];
                $cs++;
            }
            if(empty($list)){
                $list = array();
            }
            $data['data'] = $list;
            $data['sum'] = $sum / 1000;
            $data['cs'] = $cs;
        }
        return $data;
    }


    /***
    *
    *物资相关接口
    *
    ***/


    /***
    *
    *保存物资
    *
    ***/
    public function goodsSave(){
        $pd = input();
        $goodsId = 0;
        if(isset($pd['goodsId']) && !empty($pd['goodsId'])){
            $goodsId = $pd['goodsId'];
        }
        if(!isset($pd['goodsName']) || empty($pd['goodsName'])){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new GM();
        unset($pd['goodsId']);
        if(empty($goodsId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('goodsId'=>$goodsId))->update($pd);
            $pd['goodsId'] = $goodsId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除物资
    *
    ***/
    public function goodsDel(){
        $goodsId = input('goodsId/d');
        if(empty($goodsId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $goodsId;
            return $data;
            exit;
        }
        $m = new GM();
        $rs = $m->where(array('goodsId'=>$goodsId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }



    /***
    *
    *发货位置接口
    *
    ***/


    /***
    *
    *保存发货位置
    *
    ***/
    public function positionSave(){
        $pd = input();
        $positionId = 0;
        if(isset($pd['positionId']) && !empty($pd['positionId'])){
            $positionId = $pd['positionId'];
        }
        if(!isset($pd['position']) || empty($pd['position'])){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new PM();
        unset($pd['positionId']);
        if(empty($positionId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('positionId'=>$positionId))->update($pd);
            $pd['positionId'] = $positionId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除发货位置
    *
    ***/
    public function positionDel(){
        $positionId = input('positionId/d');
        if(empty($positionId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $positionId;
            return $data;
            exit;
        }
        $m = new PM();
        $rs = $m->where(array('positionId'=>$positionId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }



     /***
    *
    *发运路线接口
    *
    ***/


    /***
    *
    *保存发运路线
    *
    ***/
    public function routeSave(){
        $pd = input();
        $routeId = 0;
        if(isset($pd['routeId']) && !empty($pd['routeId'])){
            $routeId = $pd['routeId'];
        }
        if(!isset($pd['route']) || empty($pd['route'])){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $pd;
            return $data;
            exit;
        }
        $m = new RM();
        unset($pd['routeId']);
        if(empty($routeId)){
            $model = 0;
            $rs = $m->create($pd);
            if(empty($rs)){
                $data['status'] = -1;
                $data['msg'] = $m->getError();
                $data['data'] = $pd;
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $rs;
            $data['model'] = $model;
            return $data;
            exit;
        }else{
            $model = 1;
            $rs = $m->where(array('routeId'=>$routeId))->update($pd);
            $pd['routeId'] = $routeId;
            $err = $m->getError();
            if(empty($rs) && !empty($err)){
                $data['status'] = -1;
                $data['msg'] = $err;
                $data['data'] = '';
                return $data;
                exit;
            }
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $pd;
            $data['model'] = $model;
            return $data;
            exit;
        }
    }


    /***
    *
    *删除发运路线
    *
    ***/
    public function routeDel(){
        $routeId = input('routeId/d');
        if(empty($routeId)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $routeId;
            return $data;
            exit;
        }
        $m = new RM();
        $rs = $m->where(array('routeId'=>$routeId))->delete();
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = $m->getError();
            $data['data'] = $rs;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $rs;
        return $data;
        exit;
    }




    /***
    *
    *编辑流水号
    *
    ***/
    public function serialnumberSave(){
        $number = input('serialNumber/d');
        if(empty($number)){
            $data['status'] = -1;
            $data['msg'] = '参数错误';
            $data['data'] = $number;
            return $data;
            exit;
        }
        $dstr = date('Y-m-d');
        $tmp = Db::name('serialnumber')->where(array('serialDate'=>$dstr))->find();
        if(empty($tmp)){
            $data['status'] = -1;
            $data['msg'] = '当天流水号还未创建,请联系管理员或直接过磅';
            $data['data'] = $number;
            return $data;
            exit;
        }
        if($number <= $tmp['serialNumber']){
            $data['status'] = -1;
            $data['msg'] = '请求修改的流水号小于或等于现在的流水号';
            $data['data'] = $number;
            return $data;
            exit;
        }
        $rs = Db::name('serialnumber')->where(array('serialDate'=>$dstr))->update(array('serialNumber'=>$number));
        if(empty($rs)){
            $data['status'] = -1;
            $data['msg'] = '保存数据时出错';
            $data['data'] = $number;
            return $data;
            exit;
        }
        $data['status'] = 1;
        $data['msg'] = 'ok';
        $data['data'] = $number;
        return $data;
    }


    /***
    *
    *手工补单
    *
    ***/
    public function insertByHand(){
        /*$fileKey = key($_FILES);
        $file = request()->file($fileKey);
        if($file===null){
            $data['status'] = -2;
            $data['msg'] = '上传文件不存在或超过服务器限制';
            $data['data'] = $_FILES;
            $data['err'] = [];
            return $data;
        }
        $validate = new \think\Validate([
            ['fileExt','fileMime:xlsx','只允许上传xlsx类型的文件'],
            ['fileExt','fileExt:xlsx','只允许上传后缀为xlsx的文件'],
            ['fileSize','fileSize:2097152','文件大小超出限制'],
        ]);
        $data = ['fileMime'  => $file,
                 'fileSize' => $file,
                 'fileExt'=> $file
                ];
        $if = $file->getInfo();
        $info = $file->validate(['ext'=>'xlsx'])->rule('uniqid')->move(ROOT_PATH.'/public/upload/insertbills/'.date('Y-m-d'),$if['name']);
        //保存路径
        $filePath = $info->getPathname();
        $filePath = str_replace(ROOT_PATH,'',$filePath);
        $filePath = str_replace('\\','/',$filePath);
        $name = $info->getFilename();
        $filePath = str_replace($name,'',$filePath);
        if($info){
            $data['status'] = 1;
            $data['msg'] = 'ok';
            $data['data'] = $if;
            return $data;
        }else{
            //上传失败获取错误信息
            $data['status'] = -2;
            $data['msg'] = $file->getError();
            $data['data'] = [];
            $data['err'] = [];
            return $data;
        }*/
        $rs = insertBillsByHand();
        return $rs;
    }


    public function uploadimg(){
        $rs = TXUploadFile();
        print_r(json_encode($rs));
    }

}